/**
 * Created by yujia on 2017/11/30.
 */

var exam = {
    classify: null,
    type: null,
    title: null,
    answera: null,
    answerb: null,
    answerc: null,
    answerd: null,
    answer: null
}

$(".dropdown-menu a").click(function () {
    $("#classify").html($(this).text());
});

$("#add").click(function () {
    examCheck();
    alert($("#classify").text());
    $.ajax({
        type: 'POST',
        url: "/exam/add.json",
        contentType: "application/json;",
        data: JSON.stringify(exam),
        success: function () {
            alert("success");
        }, error: function (xhr, type) {
            $(location).attr('href', '/views/result.html');
        }
    });
});

function examCheck() {
    //题目
    if ($("#title").val() == "") {
        alert("题目不能为空");
    } else {
        exam.title = $.trim($("#title").val());
    }
    //答案
    if ($(".radio input").val() == "judge") {
        exam.answera = "";
        exam.answerb = "";
        exam.answerc = "";
        exam.answerd = "";
    } else if ($(".radio input").val() == "choice") {
        answera = $.trim($("#answerA").val());
        answerb = $.trim($("#answerB").val());
        answerc = $.trim($("#answerC").val());
        answerd = $.trim($("#answerD").val());
    }
    exam.classify = $.trim($("#classify").text());
    exam.type = $(".radio input").val();
    exam.answer = String($("#answer").val()).toUpperCase();
}